package com.emep.changzhi.analyse.http.aqi;

import com.emep.changzhi.analyse.entity.aqi.Weather;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.repository.aqi.WeatherRepository;
import com.emep.changzhi.analyse.service.aqi.WeatherService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yangj
 * @version V1.0
 * @Description: DOTO
 * @date 2018/3/26  15:15
 */

@RestController
@RequestMapping("weather")
@Api(value = "weatherController",tags = "天气控制器")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    /**
     * 新增天气
     * */
    @ApiOperation(value="新增天气", notes="新增天气")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "temp", value = "实时温度", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "windDirection", value = "实时风向", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "add")
    public ResultBean<Weather> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "temp","windDirection");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.boolAdd(weatherService.save(payload)) ;
        }
        return  result;
    }

    /**
     * 获取天气情况
     * */
    @GetMapping(value = "now")
    public ResultBean<Weather> findweather(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "cityCode");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            List<Weather> weathers = weatherService.getweather(payload.get("cityCode").toString());
            return result.ok(weathers.get(0));
        }
        return  result;
    }

}
